package com.guoguo.cloudmusic.logic.network

import com.guoguo.cloudmusic.BuildConfig
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 *  创建 Retrofit构造器
 * 日志参考 : https://blog.csdn.net/yu540135101/article/details/113034404
 * @author GuoGuo
 * @date 2021/12/15
 */

object ServiceCreator {
    private var okHttpClient: OkHttpClient

    init {
        val client = OkHttpClient.Builder()
        //header中追加参数
//        client.addInterceptor(AppendHeaderParamInterceptor())
        //url追加参数
        client.addInterceptor(AppendUrlParamInterceptor())
        //在Debug模式下设置日志拦截器
        if (BuildConfig.DEBUG) {
            val logger =
                HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }
            client.addInterceptor(logger)
        }
        okHttpClient = client.build()
    }

    private const val BASE_URL = "https://music.gspguoguo.top"

    // Retrofit构造器 生成Retrofit实例
    private val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    // 对外提供的方法
    fun <T> create(serviceClass: Class<T>): T = retrofit.create(serviceClass)
    inline fun <reified T> create(): T = create(T::class.java)
}

/**
 * url追加参数
 */
class AppendUrlParamInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        //1.获取到请求
        val request = chain.request()
        //2.得到之前的url
        val builder = request.url.newBuilder()
        //3.追加信息，得到新的url
        val newUrl = builder
            .addQueryParameter("cookie","MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/eapi/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/weapi/feedback;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/neapi/feedback;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/weapi/clientlog;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/weapi/clientlog;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/api/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/weapi/feedback;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/wapi/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/eapi/clientlog;;__remember_me=true; Max-Age=1296000; Expires=Sat, 18 Dec 2021 09:35:19 GMT; Path=/;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/api/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/openapi/clientlog;;MUSIC_U=3c5b80800af5ef1bb2c3fe95678722d08fece0359caa330a07ff3abff263fd108a08bd5bf851808fd78b6050a17a35e705925a4e6992f61d07c385928f88e8de; Max-Age=1296000; Expires=Sat, 18 Dec 2021 09:35:19 GMT; Path=/;;__csrf=03a11012c17f84a5a49a7220484ca92c; Max-Age=1296010; Expires=Sat, 18 Dec 2021 09:35:29 GMT; Path=/;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/eapi/clientlog;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/wapi/clientlog;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/neapi/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/wapi/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/neapi/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/api/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/api/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/eapi/feedback;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/neapi/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/wapi/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/openapi/clientlog;")
            .addQueryParameter("token", "3c5b80800af5ef1bb2c3fe95678722d08fece0359caa330a4edf5eb51bf84b438a08bd5bf851808fd78b6050a17a35e705925a4e6992f61d07c385928f88e8de")
            .build()
        //4.新的url创建新的request
        val newRequest = request.newBuilder()
            .url(newUrl)
            .build()
        //5.返回
        return chain.proceed(newRequest)
    }

}

/**
 * header追加参数
 */
class AppendHeaderParamInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        //1.得到request
        val request = chain.request()
        //2.得到之前的header
        val builder = request.headers.newBuilder()
        //3.追加新的header信息
        val newHeader = builder
            .add("cookie","MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/eapi/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/weapi/feedback;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/neapi/feedback;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/weapi/clientlog;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/weapi/clientlog;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/api/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/weapi/feedback;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/wapi/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/eapi/clientlog;;__remember_me=true; Max-Age=1296000; Expires=Sat, 18 Dec 2021 09:35:19 GMT; Path=/;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/api/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/openapi/clientlog;;MUSIC_U=3c5b80800af5ef1bb2c3fe95678722d08fece0359caa330a07ff3abff263fd108a08bd5bf851808fd78b6050a17a35e705925a4e6992f61d07c385928f88e8de; Max-Age=1296000; Expires=Sat, 18 Dec 2021 09:35:19 GMT; Path=/;;__csrf=03a11012c17f84a5a49a7220484ca92c; Max-Age=1296010; Expires=Sat, 18 Dec 2021 09:35:29 GMT; Path=/;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/eapi/clientlog;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/wapi/clientlog;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/neapi/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/wapi/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/neapi/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/api/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/api/feedback;;MUSIC_A_T=1537101271776; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/eapi/feedback;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/neapi/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/wapi/clientlog;;MUSIC_R_T=1537892666865; Max-Age=2147483647; Expires=Wed, 21 Dec 2089 12:49:26 GMT; Path=/openapi/clientlog;")
            .add("token", "3c5b80800af5ef1bb2c3fe95678722d08fece0359caa330a07ff3abff263fd108a08bd5bf851808fd78b6050a17a35e705925a4e6992f61d07c385928f88e8de")
            .build()
        //4.创建一个新的请求
        val newRequest = request.newBuilder()
            .headers(newHeader)
            .build()
        //5.返回
        return chain.proceed(newRequest)
    }

}